package Epic;


/**
 * Given two arrays A, B, each contains elements of digits, return an array of A - B.
 * Your machine can only do calculation of less than 20.
 * 
 * eg. A = [1,2,5,7,5];
	   B = [3,4,8,9];
	   A - B = [9,0,8,6];
 * @author Ray
 *
 */
public class LongSubtraction {

	public LongSubtraction(){
		
	}
	
	public static int[] subStraction(int[] a, int[] b){
		int aLen = a.length;
		int bLen = b.length;
		int[] c;
		c = new int[a.length];
		
		int borrow = 0;
		for(int i=0; i<a.length; i++) {
			int sub = a[a.length-1-i]-borrow - (i>=b.length? 0 : b[b.length-1-i]);
			borrow = 0;
			if(sub < 0) {
				sub += 10;
				borrow = 1;
			}
			c[c.length-1-i] = sub;
		}
	
		c = adjust(c);
		return c;
	}		
	
	private static int[] adjust(int[] c){
		int i;
		for(i=0;i<c.length;++i){
			if(c[i]!=0) break;
		}
		int[] ret = new int[c.length-i];
		for(int j=0;j<ret.length;++j){
			ret[j]=c[i++];
		}
		return ret;
	}
		
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int A[] = {5,7,5};
		int B[] = {3,4,8,9};
		
		int C[];
		
		C = subStraction(A, B);
		
		for(int i=0; i<C.length; i++)
			System.out.print(C[i]+" ");
		System.out.println("");
	}

}
